<template>
  <div class="list">
    <van-nav-bar title="商品列表" left-arrow fixed @click-left="$router.back()" />
    <van-search readonly shape="round" placeholder="搜索商品" @click="$router.push('/search')" />

    <div class="sort">
        <span class="sort-item" @click="sortByAll" :class="{selected:n===0}">综合</span>
        <span class="sort-item" @click="sortBySale" :class="{selected:n===1}">销量</span>

        <span class="sort-item" @click="sortByPrice">
            <span :class="{selected:n===2}">价格</span>
            <div class="arrow">
                <van-icon name="arrow-up" size="12px" :class="{selected:m===1}" />
                <van-icon name="arrow-down" size="12px" :class="{selected:m===2}" />
            </div>
        </span>
    </div>

    <div class="items">
        <GoodsItem :item="item" v-for="item in list" :key="item.goods_id"></GoodsItem>
    </div>
  </div>
</template>

<script>

import GoodsItem from '@/components/GoodsItem'
import { getGoodsList } from '@/api/goods'

export default {
  data () {
    return {
      n: 0,
      m: 0,
      list: []
    }
  },
  methods: {
    async getGoods (obj) {
      const { sortType, sortPrice } = obj
      const goodsName = this.$route.query.keyword
      const categoryId = this.$route.query.categoryId
      const { data: { list } } = await getGoodsList({ goodsName, sortType, categoryId, sortPrice })
      this.list = list.data
    },
    sortByAll () {
      this.n = 0
      this.m = 0
      this.getGoods({ sortType: 'all', sortPrice: null })
    },
    sortBySale () {
      this.n = 1
      this.m = 0
      this.getGoods({ sortType: 'sales', sortPrice: null })
    },
    sortByPrice () {
      this.n = 2
      this.m = this.m < 2 ? this.m + 1 : 1
      this.getGoods({ sortType: 'price', sortPrice: this.m - 1 })
    }
  },
  components: {
    GoodsItem
  },
  async created () {
    this.getGoods({ sortType: 'all', sortPrice: null })
  }

}
</script>

<style lang="less" scoped>
.list {
 padding-top: 46px;
 background-color: white;
}

.van-nav-bar {
    ::v-deep .van-nav-bar__title {
    font-weight: bold;
  }

    ::v-deep .van-icon-arrow-left {
        color: black;
    }
}

.sort {
    width: 375px;
    height: 25px;
    padding: 10px 0;
    display: flex;
    justify-content: space-between;
    line-height: 25px;

    .sort-item {
        width: 125px;
        height: 25px;
        text-align: center;
        font-size: 14px;
        color: #000;
    }

    .sort-item:last-child {
        position: relative;

        span {
            position: absolute;
            left: 40px;
        }

        .arrow {
            width: 12px;
            height: 22px;
            position: absolute;
            left: 75px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
    }

    .selected {
        color: #e49a3d !important
    }

}

</style>
